{% extends "instructor_portal/course_base.html" %}
{% load instructor_portal_extras %}
{% load student_portal_extras %} {# for getTrClass #}

{% block extrahead %}
<script type="text/javascript">
function deleteGrade(gradeId) {
    if (confirm("Are you sure you want to delete this Grade?")) {
        var grade;
        grade = "deleteGrade-" + gradeId;
        document.getElementById(grade).submit();
    }
}
function deleteDDO(DDO_id) {
    if (confirm("Are you sure you want to delete this Due Date Override?")) {
        var DDO;
        DDO = "deleteDDO-" + DDO_id;
        document.getElementById(DDO).submit();
    }
}
</script>
{% endblock %}

{% block content %}

{# Page Heading #}
<h2>Grade Report</h2>

{# Grade Report Detail Information #}
<div>
    <b>User:</b> {{ enrollment.student }} <br />
    <b>Course:</b> {{ course.name }} <br />
    {# Add row for editing this Enrollment in Admin #}
    {% if access_type|editEnrollment %}
        <img src="{{ STATIC_URL }}icon_info_smallest.png" alt="Edit Enrollment" />
        <b><a href="/admin/student_portal/enrollment/{{ enrollment.id }}/">
            Edit or Delete this Enrollment in Admin.</a></b><br /><br />
    {% endif %}
</div>

{# Print Messages Stack #}
{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

{# Grade Report Points Summary #}
 <h3>Enrollment Point Summary</h3>
{% if point_summary %}
 <div>
    <b>Points Earned so Far:</b> {{ point_summary.0 }} <br />
    <b>Points Possible:</b> {{ point_summary.1 }} <br />
    <b>Points Extra Credit:</b> {{ point_summary.2 }} <br />
</div>
{% else %}
<div><em>Point Summary is Unavailable.</em></div>
{% endif %}

{# List ExtraCredit #}
<h3>Extra Credit</h3>
    {% if access_type|editExtraCredit %}
        <img src="{{ STATIC_URL }}icon_add_smallest.png" alt="Edit Enrollment" />
        <b><a href="/admin/student_portal/extracredit/add/?enrollment={{ enrollment.id }}">
            Add new Extra Credit item in Admin.</a></b><br />
    {% endif %}
{% if extra_credit %}
    <ul>
    {% for ec in extra_credit %}
        <li><b>{{ ec.return_date }}</b> -- {{ ec.name }}: {{ ec.points_earned }} points.</li>
    {% endfor %}
    </ul>
{% else %}
<em><h4>This student has not received extra credit.</h4></em>
{% endif %}

{# List Assignments #}
<h3>Course Assignments</h3>
{% if assignment_list.object_list %}
<div>
<b>Please Note:</b> Download links are time sensitive for security so you may need to reload the page for a fresh link.
You can get extended Assignment details and management actions for authorized users on the Course Assignments page.
</div>

{# Configure Table #}
<table summary="Current Assignments" class="assignments" cellspacing="0">
<thead>
    <tr>
        <th>Name</th>
        <th>Due Date</th>
        <th>Grading</th>
    </tr>
</thead>

{####################################################}
{# Cycle through the course assignment data stored in assignment_list #}
{# assignment_list is a complete list of elements that look like this:  #}
{# [0=Assignment, 1=Submission, 2=Grade, 3=DueDateOverride] #}
{####################################################}
{% for assignment_data in assignment_list.object_list %}

{# First Row is for Assignment Header Details #}
<tr class={{ assignment_data|getTrClass|safe }}>

    {# Assignment Name #}
    <td><b>{{ assignment_data.0.name }}</b></td>

    {# Assignment Due Date #}
    <td><b>
    {% if assignment_data.3 %} {# DueDateOverride in effect: calculate based on that. #}
        {{ enrollment.start_date|adjustDateWeeks:assignment_data.3.weeks_after|adjustDateDays:assignment_data.3.days_after|date:"M jS Y" }}
    {% else %} {# Calculate based on Assignment Default #}
        {% if assignment_data.0.apply_due_date %}
            {{ enrollment.start_date|adjustDateWeeks:assignment_data.0.weeks_after|adjustDateDays:assignment_data.0.days_after|date:"M jS Y" }}
        {% else %}
            None Listed
        {% endif %}
    {% endif %}
   </b></td>

   {# Assignment Grading #}
   <td><b>
        {# Display points earned #}
        {% if assignment_data.2 %}
            {{ assignment_data.2.points_earned }} /
        {% else %}
            -- /
        {% endif %}
        {# Display points possible #}
        {% if assignment_data.0.display_points_possible %}
            {{ assignment_data.0.points_possible }}
        {% else %}
            --
        {% endif %}
    </b></td>
</tr> {# End First Row. #}

{# Second row is for student file Submissions #}
{% if assignment_data.1 %} {# Student has a Submission--display timestamps and Download Link. #}
<tr class={{ assignment_data|getTrClass|safe }}>
    <td colspan="3"> <em><b>Submitted:</b></em> {{ assignment_data.1.date|date:"M jS Y" }} -
        {% if assignment_data.1.on_time %}
            <b>On Time</b>
        {% else %}
            <b>Late</b>
        {% endif %}
        <br />
        <img class="icon" src="{{ STATIC_URL }}icon_download_small.png" alt="Download" />
        <em><a href="{{ assignment_data.1.file.url|safe }}">Download Submission</a></em> <br />
    </td>
</tr>
{% else %} {% if assignment_data.0.disable_submissions %} {# No Student Submission--Print proper status message. #}
<tr class={{ assignment_data|getTrClass|safe }}>
    <td colspan="3"><b>Online Submissions Disabled for this Assignment.</b></td>
</tr> {% else %}
<tr class={{ assignment_data|getTrClass|safe }}>
    <td colspan="3"><b>Student has not made this Submission.</b></td>
</tr> {% endif %}
    {% if assignment_data.3 %} {# Display data on existing DueDateOverrides. #}
    <tr class={{ assignment_data|getTrClass|safe }}>
        <td colspan="3"><b>Due Date Override in effect.</b><br />
            Without one, the default Due Date would have been: <br />
        {% if assignment_data.0.apply_due_date %}
            {{ enrollment.start_date|adjustDateWeeks:assignment_data.0.weeks_after|adjustDateDays:assignment_data.0.days_after|date:"M jS Y" }}
        {% else %}
            This Assignment does not have default Due-Date.
        {% endif %}
        </td>
    </tr>
    {# Display Delete link with existing DueDateOverride. #}
    <tr class={{ assignment_data|getTrClass|safe }}><td colspan="3">
    <form id="deleteDDO-{{ assignment_data.3.id }}" method="post"
        action="/instructor/courseindex/{{ assignment_data.0.course.id }}/roster_tools/{{ enrollment.id }}/deleteDDO/{{ assignment_data.3.id }}/">
    {% csrf_token %}</form>
    <img class="icon" src="{{ STATIC_URL }}icon_delete_smallest.png" alt="DeleteDueDateOverride" />
    <a href="javascript:deleteDDO( {{ assignment_data.3.id }} )">Delete this DueDateOverride?</a>
    </td></tr>
    {# Display Add link when no existing DueDateOverride or Grade and online submissions are enabled. #}
    {% else %}{% if not assignment_data.2 %}{% if not assignment_data.0.disable_submissions %}
    <tr class={{ assignment_data|getTrClass|safe }}>
    <td colspan="3">
        <img class="icon" src="{{ STATIC_URL }}icon_add_smallest.png" alt="AddDueDateOverride" />
        <a href="/instructor/roster_tools/{{ enrollment.id }}/grade_report/addDDO/{{ assignment_data.0.id }}/">
            Add Due Date Override.</a>
    </td></tr>
    {% endif %}{% endif %}{% endif %}
{% endif %} {# End Submission Related Output #}

{# Finally, Add rows for Assignment Grading #}
{% if assignment_data.2 %}
    {# Grader Comments #}
    {% if assignment_data.2.comments %}
        <tr class={{ assignment_data|getTrClass|safe }}>
            <td> <em><b>Grader Comments:</b></em> </td>
            <td class="assignmentDesc" colspan="2"> {{ assignment_data.2.comments }} </td>
        </tr>
    {% endif %}
    {# Returned Files #}
    {% if assignment_data.2.returned_files %}
        <tr class={{ assignment_data|getTrClass|safe }}>
            <td> <em><b>Returned File:</b></em> </td>
            <td colspan="2">
                <img class="icon" src="{{ STATIC_URL }}icon_download_small.png" alt="Download" />
                <b><a href="{{ assignment_data.2.returned_files.url|safe }}">Download Returned File</a></b> <br />
            </td>
        </tr>
    {% endif %}
    {# Edit Grade Link #}
    <tr class={{ assignment_data|getTrClass|safe }}><td colspan="3">
    <img class="icon" src="{{ STATIC_URL }}icon_info_smallest.png" alt="Edit Grade" />
    <b><a href="/admin/student_portal/grade/{{ assignment_data.2.id }}/">Edit this Grade in admin?</a></b><br />
    </td></tr>
    {# Delete Grade Link #}
    <tr class={{ assignment_data|getTrClass|safe }}><td colspan="3">
    <form id="deleteGrade-{{ assignment_data.2.id }}" method="post"
          action="delete/grade/{{ assignment_data.2.id }}/">
        {% csrf_token %}
    </form>
    <img class="icon" src="{{ STATIC_URL }}icon_delete_smallest.png" alt="Delete Grade" />
    <b><a href="javascript:deleteGrade( {{ assignment_data.2.id }} )">Delete this Grade?</a></b><br />
    </td></tr>
{% else %} {# Add row for Return Grade #}
    <tr class={{ assignment_data|getTrClass|safe }}>
        <td colspan="3">
            <img class="icon" src="{{ STATIC_URL }}icon_return_small.png" alt="ReturnGrade" />
            <a href="/instructor/roster_tools/{{ enrollment.id }}/return/grade/{{ assignment_data.0.id }}/">
            Return Grade</a>
        </td>
    </tr>
{% endif %}

{# Row Separator #}
{% if not forloop.last %}
   <tr class="assignmentBlank"><td colspan="3"></td></tr>
{% endif %}

{% endfor %}
</table> {# End of assignment_list #}
{# Assignment Page Pagination #}
<div class="pagination">
    <span class="step-links">
        {% if assignment_list.has_previous %}
            <a href="?page={{ assignment_list.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ assignment_list.number }} of {{ assignment_list.paginator.num_pages }}.
        </span>

        {% if assignment_list.has_next %}
            <a href="?page={{ assignment_list.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>
{% else %}
<em><h4>No Assignments Listed</h4></em>
{% endif %}

{% endblock %}

